#include <iostream>
#include <fstream>
#include <unordered_map>
#include <string>


class EngExplain{
    std::unordered_map<std::string,std::string> _dict;

    void load_ByEng()
    {
        std::ifstream ifs("EnglishWord.txt",std::ifstream::in);
        std::cout<<std::boolalpha<<ifs.is_open()<<std::endl;
        std::string tmp;
        std::string word;

        getline(ifs,tmp);//保证本次读取是第一个的单词
        int cnt = -1;
        while(!ifs.eof())
        {
            cnt = 1;
            word.clear();
            word = tmp;
            tmp.clear();
            while(!ifs.eof()){
                getline(ifs,tmp);
                if(tmp.find_first_of('\t')!=std::string::npos)
                {
                    //下一行的开始存在在tmp里面
                    break;
                }
                else
                {
                    cnt++;
                    word += '\n' + tmp;
                }
            }
            if(cnt==1)
            {
                int space = 0;
                while(space<word.size())
                {
                    if(word[space] == '\t')
                    {
                        break;
                    }
                    space++;
                }

                int explain = space;
                while(explain<word.size())
                {
                    if(word[explain] != '\t')
                    {
                        break;
                    }
                    explain++;
                }
                _dict[word.substr(0,space)] = word.substr(explain);
            }
            else
            {
                int space = 0;
                while(space<word.size())
                {
                    if(word[space] == '\t')
                    {
                        break;
                    }
                    space++;
                }
                int explain = space;
                while(explain<word.size())
                {
                    if(word[explain]=='\"')
                    {
                        break;
                    }
                    explain++;
                }

                _dict[word.substr(0,space)] = word.substr(explain+1,word.size()-explain -2);
            }
        }
    }
    void load_ByZh()
    {
        std::ifstream ifs("Eng-Zh.txt",std::ifstream::in);
        std::cout<<std::boolalpha<<ifs.is_open()<<std::endl;
        std::string tmp;
        std::string word;

        getline(ifs,tmp);//保证本次读取是第一个的单词
        int cnt = -1;
        while(!ifs.eof())
        {
            cnt = 1;
            word.clear();
            word = tmp;
            tmp.clear();
            while(!ifs.eof()){
                getline(ifs,tmp);
                if(tmp.find_first_of('\t')!=std::string::npos)
                {
                    //下一行的开始存在在tmp里面
                    break;
                }
                else
                {
                    cnt++;
                    word += '\n' + tmp;
                }
            }
            if(cnt==1)
            {
                int space = 0;
                while(space<word.size())
                {
                    if(word[space] == '\t')
                    {
                        break;
                    }
                    space++;
                }

                int explain = space;
                while(explain<word.size())
                {
                    if(word[explain] != '\t')
                    {
                        break;
                    }
                    explain++;
                }
                _dict[word.substr(0,space)] = word.substr(explain);
            }
            else
            {
                int space = 0;
                while(space<word.size())
                {
                    if(word[space] == '\t')
                    {
                        break;
                    }
                    space++;
                }
                int explain = space;
                while(explain<word.size())
                {
                    if(word[explain]=='\"')
                    {
                        break;
                    }
                    explain++;
                }

                _dict[word.substr(0,space)] = word.substr(explain+1,word.size()-explain -2);
            }
        }
    }
    public:
    EngExplain()
    {
        //加载词书
        load_ByZh();
        int testInt;
    }
    std::string operator()(const std::string word)const
    {
        auto it  = _dict.find(word);
        if(it == _dict.end())
        {
            return "No this word: \"" + word + "\" in this Dictionary\n";
        }
        return it->second;
    }
    
};